// %%%%%%%%%%%%%%%%%%%%%%% PROCUTIL.H %%%%%%%%%%%%%%%%%%%%%%%
// Supplies functions assisting in finding and manipulating processes.
// ----------------------------------------------------------

#include "globals.h"

#include <Windows.h>
#include <TlHelp32.h>
#pragma once

//
// Searches through the list of registered processes.
// If a process sharing the name given is found, returns the process Id of that instance.
// Else, returns NULL.
//

DWORD const getPidFromExe( char const * const );

//
// Attempts to find the window of the given title.
// If found, returns the process Id of that window's thread.
// Else, returns NULL.
//

DWORD const getPidFromWndTitle( char const * const );

//
// Prioritized search by name in this order:
//	1. getPidFromExe
//	2. getPidFromWindowTitle
// If no process Id was found corresponding to the name, returns NULL.
//

DWORD const getPid( char const * const );

//
// Reads memory from a process given the:
//	Process HANDLE
//	address
//	number of bytes to read
//	buffer
// Returns 0 only on error.
// Note: Requires that the HANDLE to the process has atleast PROCESS_VM_READ access.
//

BOOL const __fastcall readMemory( HANDLE, DWORD const, DWORD const, void *const );

//
// Writes memory to a process given the:
//	Process HANDLE
//	address
//	number of bytes to write
//	input buffer
// Returns 0 only on error.
// Note: Requires that the HANDLE to the process has atleast PROCESS_VM_WRITE and PROCESS_VM_OPERATATION access.
//

BOOL const __fastcall writeMemory( HANDLE, DWORD const, DWORD const, void *const );

//
// Creates a remote thread in the target process given by the HANDLE.
// Returns a HANDLE to the created thread.
// Creates the thread in a suspended state.
// Note: Requires that the HANDLE to the process has at least (access rights):
//	PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ
//

HANDLE __fastcall makeRemoteSuspendedThread( HANDLE );

//
// Resumes the thread given by the HANDLE and waits for it to end.
// If the second parameter is true, this function closes the HANDLE before returning.
//

void __fastcall resumeAndWait( HANDLE, bool const );

// 
// Opens a process by its process Id and returns a HANDLE to it.
// An optional parameter can specify the access rights with which to open the process.
//

HANDLE __fastcall openProcess( DWORD const, 
	DWORD desiredAccess = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ );